package net.flowas.genericcrud.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class CommonFunction {
  private static final Logger LOG = LoggerFactory.getLogger(CommonFunction.class);  
  private CommonFunction() {}
  /**
   * 解析xml文件.
   * @param url 文件位置
   * @return
   */
  public static Element parceXmlConfig(URL url) {
    InputStream in = null;
    Document doc;
    try {
      in = url.openStream();
      doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
    } catch (SAXException | IOException | ParserConfigurationException e) {
      LOG.error("Failed to load file:" + url.toExternalForm(), e);
      return null;
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          LOG.error("Failed to close resource:" + url.toExternalForm(), e);
        }
      }
    }
    return doc.getDocumentElement();
  }
}
